/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package replic.sla;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 *
 * @author Leonardo Oliveira Moreira
 * 
 * Class used to read the contents of the SLA
 */
public final class SLA {

    private SLA() {
    }

    /**
     * Method used to retrieve a list of database services described in SLA
     * @return 
     */
    public static List<DBService> getVirtualMachineList() {
        List<DBService> result = new ArrayList<DBService>();
        SAXBuilder builder = new SAXBuilder();
        Document document = null;
        try {
            document = builder.build(SLA.class.getResourceAsStream("sla.xml"));
            List dbServiceList = document.getRootElement().getChildren();
            for (int i = 0; dbServiceList != null && i < dbServiceList.size(); i++) {
                Element dbService = (Element) dbServiceList.get(i);
                DBService dbServiceTemp = new DBService();
                dbServiceTemp.setDbName(dbService.getChildText("db_name"));
                dbServiceTemp.setResponseTime(Long.parseLong(dbService.getChildText("response_time")));
                dbServiceTemp.setThroughput(Long.parseLong(dbService.getChildText("throughput")));
                dbServiceTemp.setConsistencyLevel(Long.parseLong(dbService.getChildText("consistency_level")));
                result.add(dbServiceTemp);
            }
        } catch (JDOMException ex) {
            Logger.getLogger(SLA.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(SLA.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    public static List<DBService> getVirtualMachineList(String filePath) throws IOException {
        List<DBService> result = new ArrayList<DBService>();
        SAXBuilder builder = new SAXBuilder();
        Document document = null;
        try {
            document = builder.build(new File(filePath));
            List dbServiceList = document.getRootElement().getChildren();
            for (int i = 0; dbServiceList != null && i < dbServiceList.size(); i++) {
                Element dbService = (Element) dbServiceList.get(i);
                DBService dbServiceTemp = new DBService();
                dbServiceTemp.setDbName(dbService.getChildText("db_name"));
                dbServiceTemp.setResponseTime(Long.parseLong(dbService.getChildText("response_time")));
                dbServiceTemp.setThroughput(Long.parseLong(dbService.getChildText("throughput")));
                dbServiceTemp.setConsistencyLevel(Long.parseLong(dbService.getChildText("consistency_level")));
                result.add(dbServiceTemp);
            }
        } catch (JDOMException ex) {
            Logger.getLogger(SLA.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

}